/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcPurContractService;
import com.vtuzx.kingdee.xqc_api.service.IXqcPurPackageService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import jdk.nashorn.internal.parser.Token;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 采购入库(包材)
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/pac_in")
@RestController
public class XqcPacInController {
    /**
     * 采购服务
     */
    @Autowired
    private IXqcPurPackageService purPackageService;



    /**
     * 获取采购订单
     */
    @PostMapping("/get_purOrder_bill")
    @VtuzxApi
    public Object getPurConBillDetail(@VtuzxControlParam VtuzxToken token)  {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        return purPackageService.getPurOrderBill();
    }

    /**
     * 获取采购订单详情
     */
    @PostMapping("/get_purOrder_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "orderNo", label = "单号", notNull = true)
    } )
    public Object getPurConBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        return purPackageService.getPurOrderBillDetail(orderNo);
    }



    /**
     * 创建采购入库结算单
     */
    @PostMapping("/create_pur_in_settlement_bill")
    @VtuzxApi
    public Object createPurInSettlementBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token, "name");
        purPackageService.createPurInSettlementBill(_userId, userName, param);
        return null;
    }



    /**
     * 审核采购入库调拨单
     */
    @PostMapping("/audit_purOrder_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "orderNo", label = "单号", notNull = true)
    } )
    public void auditPurOrderBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = token.getString("name");
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        purPackageService.auditPurOrderBill(_userId, userName, orderNo);
    }

    /**
     * 更新数量
     */
    @PostMapping("/update_purOrder_count")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true)
    } )
    public VtuzxMap updatePurOrderCount(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = token.getString("name");
        String id = VtuzxUtil.getString(param, "id");
        int count = VtuzxUtil.getInt(param, "count");
        return purPackageService.updatePurOrderCount(_userId, userName, id, count);
    }

    /**
     * 获取采购入库结算单列表
     */
    @PostMapping("/get_pur_in_settlement_list")
    @VtuzxApi
    public Object getPurInSettlementList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token, "name");
        return purPackageService.getPurInSettlementList(_userId, param, userName);
    }

    /**
     * 获取采购入库结算单详细
     */
    @PostMapping("/get_pur_in_settlement_detail")
    @VtuzxApi
    public Object getPurInSettlementDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token, "name");
        return purPackageService.getPurInSettlementDetail(_userId, param, userName);
    }

    /**
     * 结算
     */
    @PostMapping("/settlement_material")
    @VtuzxApi
    public Object settlementMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token, "name");
        purPackageService.settlementMaterial(_userId, param, userName);
        return null;
    }

    /**
     * 反结算
     */
    @PostMapping("/un_settlement_material")
    @VtuzxApi
    public Object unSettlementMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token, "name");
        purPackageService.unSettlementMaterial(_userId, param, userName);
        return null;
    }

}
